package com.tanhua.server.controller;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.common.vo.ErrorResult;
import com.tanhua.server.service.QXSoundService;
import com.tanhua.server.vo.SoundVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("peachblossom")
public class SoundController {

    @Autowired
    private QXSoundService QXSoundService;

    /**
     * 发送语音
     *
     * @param soundFile  语音文件
     * @return
     */
    @PostMapping
    public ResponseEntity<Object> sendSound(@RequestParam("soundFile") MultipartFile soundFile){
        try {
            Boolean reslut = QXSoundService.sendSound(soundFile);
            if (reslut) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000005").errMessage("账号封禁中,无效操作!").build();
        //如果返回值为空,则说明验证失败,返回状态码500并将对应错误信息传递给用户
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
     * 接收语音
     *
     * @return
     */
    @GetMapping
    public ResponseEntity<Object> getSound(){
        try {
            SoundVo soundVo = QXSoundService.getSound();
            if (ObjectUtil.isNotEmpty(soundVo)){
                return ResponseEntity.ok(soundVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000005").errMessage("账号封禁中,无效操作!").build();
        //如果返回值为空,则说明验证失败,返回状态码500并将对应错误信息传递给用户
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
     * 喜欢
     *
     * @param likeUserId
     * @return
     */
    @GetMapping("{id}/love")
    public ResponseEntity<Object> likeUser(@PathVariable("id") Long likeUserId) {
        try {
            QXSoundService.likeUser(likeUserId);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000005").errMessage("账号封禁中,无效操作!").build();
        //如果返回值为空,则说明验证失败,返回状态码500并将对应错误信息传递给用户
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
     * 不喜欢
     *
     * @param likeUserId
     * @return
     */
    @GetMapping("{id}/unlove")
    public ResponseEntity<Object> notLikeUser(@PathVariable("id") Long likeUserId) {
        try {
            QXSoundService.notLikeUser(likeUserId);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000005").errMessage("账号封禁中,无效操作!").build();
        //如果返回值为空,则说明验证失败,返回状态码500并将对应错误信息传递给用户
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }
}
